import request from '@/utils/request'
import type { UserInfo, UserQuery } from '@/types/user'

/**
 * 获取用户列表
 */
export function getUserList(query: UserQuery) {
  return request.get('/system/user/list', query)
}

/**
 * 获取用户详情
 */
export function getUserInfo(userId?: number) {
  return request.get(userId ? `/system/user/${userId}` : '/system/user/')
}

/**
 * 新增用户
 */
export function addUser(data: UserInfo) {
  return request.post('/system/user', data, {
    showLoading: true,
    showSuccess: true,
    successMsg: '用户新增成功',
  })
}

/**
 * 修改用户
 */
export function updateUser(data: UserInfo) {
  return request.put('/system/user', data, {
    showLoading: true,
    showSuccess: true,
    successMsg: '用户修改成功',
  })
}

/**
 * 删除用户
 */
export function deleteUser(userIds: number | number[]) {
  const ids = Array.isArray(userIds) ? userIds.join(',') : userIds
  return request.delete(`/system/user/${ids}`, {}, {
    showLoading: true,
    showSuccess: true,
    successMsg: '用户删除成功',
  })
}

/**
 * 重置用户密码
 */
export function resetUserPwd(userId: number, password: string) {
  return request.put('/system/user/resetPwd', {
    userId,
    password,
  }, {
    showLoading: true,
    showSuccess: true,
    successMsg: '密码重置成功',
  })
}

/**
 * 修改用户状态
 */
export function changeUserStatus(userId: number, status: string) {
  return request.put('/system/user/changeStatus', {
    userId,
    status,
  }, {
    showLoading: true,
    showSuccess: true,
    successMsg: '状态修改成功',
  })
}

/**
 * 用户密码重置
 */
export function updateUserPwd(oldPassword: string, newPassword: string) {
  return request.put('/system/user/profile/updatePwd', {
    oldPassword,
    newPassword,
  }, {
    showLoading: true,
    showSuccess: true,
    successMsg: '密码修改成功',
  })
}

/**
 * 用户头像上传
 */
export function uploadAvatar(data: FormData) {
  return request.post('/system/user/avatar', data, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
    showLoading: true,
  })
}

/**
 * 查询用户个人信息
 */
export function getUserProfile() {
  return request.get('/system/user/profile')
}

/**
 * 修改用户个人信息
 */
export function updateUserProfile(data: any) {
  return request.put('/system/user/profile', data, {
    showLoading: true,
    showSuccess: true,
    successMsg: '个人信息修改成功',
  })
}

/**
 * 查询授权角色
 */
export function getAuthRole(userId: number) {
  return request.get(`/system/user/authRole/${userId}`)
}

/**
 * 保存授权角色
 */
export function updateAuthRole(data: { userId: number; roleIds: string }) {
  return request.put('/system/user/authRole', data, {
    showLoading: true,
    showSuccess: true,
    successMsg: '授权成功',
  })
}

/**
 * 查询部门下拉树结构
 */
export function deptTreeSelect() {
  return request.get('/system/user/deptTree')
}

/**
 * 导出用户
 */
export function exportUser(query: UserQuery) {
  return request.post('/system/user/export', query, {
    responseType: 'blob',
    showLoading: true,
  })
}

/**
 * 获取用户导入模板
 */
export function importTemplate() {
  return request.post('/system/user/importTemplate', {}, {
    responseType: 'blob',
  })
}

/**
 * 导入用户
 */
export function importData(data: FormData, updateSupport: boolean) {
  return request.post(`/system/user/importData?updateSupport=${updateSupport}`, data, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
    showLoading: true,
    showSuccess: true,
    successMsg: '导入成功',
  })
}
